package com.example.mes.webSocket;
import com.alibaba.fastjson.JSON;
import com.example.mes.mapper.repository_update;
import com.example.mes.model.Respository;
import com.example.mes.service.interfase.IRespositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
@ServerEndpoint("/respository/state")
@Component
public class WebSocketServer {

    private static IRespositoryService respositoryService;
    private static repository_update repositoryUpdate;
    private boolean stop = true;
    @Autowired
    public void setPlanmecaScreenService(IRespositoryService respositoryService) {
        WebSocketServer.respositoryService = respositoryService;
    }
    @Autowired(required = false)
    public void repository_update(repository_update repositoryUpdate) {
        WebSocketServer.repositoryUpdate = repositoryUpdate;
    }

    Session session;
    @OnOpen//连接
    public void onOpen(Session session) throws IOException, EncodeException {
        System.out.println(respositoryService);
        System.out.println(session);
        this.session = session;
        System.out.println("this.session"+this.session);
        System.out.println("连接成功");
      List<Respository>list =  respositoryService.list();
      sendMessage(list);
        System.out.println("sss"+repositoryUpdate);
        new Thread(() -> {
           while(stop){
            if(repositoryUpdate.ifupdate() == 1)
            {
                List<Respository> list2 =  respositoryService.list();
                try {
                    sendMessage(list2);
                    repositoryUpdate.yes();
                    Thread.sleep(1000);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EncodeException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
           }
        }).start();

    }
    /**
     * 连接关闭调用的方法
     */
    @OnClose//关闭
    public void onClose() {
        stop = false;
        System.out.println("关闭连接");
    }
    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息*/
    @OnMessage
    public void onMessage(String message, Session session) throws IOException, EncodeException {
//        log.info("用户消息:"+userId+",报文:"+message);
        //可以群发消息
        //消息保存到数据库、redis
        System.out.println("收到消息"+message);
//        sendMessage();
        List<Respository>list =  respositoryService.list();
        sendMessage(list);
    }
    /**
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("连接错误");
    }

    /**
     * 实现服务器主动推送
     */

    public void sendMessage( List<Respository> list) throws IOException, EncodeException {
//      this.session.getBasicRemote().sendText(list);
//        System.out.println(list);
        System.out.println("session:"+session);
//        List<Respository>list1 =  respositoryService.findAll();
        System.out.println(list);
         String jonsstr = JSON.toJSONString(list);
        this.session.getBasicRemote().sendText(jonsstr);
    }
    /**
     * 发送自定义消息
     * */
    public static void sendInfo(String message) throws IOException {


    }
}
